package com.lxf.cloud.cloud_service_provider.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xf
 * Date: 2017-12-21
 * Time: 22:40
 * To change this template use Editor | File and Code Templates.
 */
@RestController
public class HelloController {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private DiscoveryClient client;
    @Autowired
    private Registration registration;

    @GetMapping(value = "/hello")
    public String index() {
        ServiceInstance instance = registration;
        LOGGER.info("/hello,host:" + instance.getHost() + ",server_id:" + instance.getServiceId());
        return "Hello World";
    }

    @GetMapping(value = "/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstances(@PathVariable String applicationName) {
        return client.getInstances(applicationName);
    }

}
